<!DOCTYPE html>
<html>

<head>
    <title>发起结伴</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap-4.1.1-dist/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap-4.1.1-dist/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/editTravels.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <script src="/js/plugins/form/jquery.form.js"></script>
    <script src="/js/plugins/eleditor/webuploader.min.js"></script>
    <script src="/js/plugins/eleditor/Eleditor.min.js"></script>
    <script src="/js/My97DatePicker/WdatePicker.js"></script>
    <script src="/js/plugins/message/jquery.bootstrap.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/checkLogin.js"></script>
    <script src="/js/common.js"></script>

</head>
<script>
    $(function () {
        var companionId = getParams().id;

        if (companionId) {
            $.get(baseUrl + "/companions/" + companionId, function (data) {
                // 使用jrender 插件渲染数据 到页面
                console.log(data);
                $(".travels").renderValues(data);
            });
        }

        //创建文本编辑器
        var Edr = new Eleditor({
            el: "#contentEditor",
            upload: {
                //图片上传的路径
                server: baseUrl + "/images"
            }
        });

        //更换封面按钮绑定点击事件
        $("#changeBtn").click(function () {
            $("#fileBtn").click();
        });

        //文件筐绑定值改变事件
        $("#fileBtn").change(function () {
            if ($(this).val()) {
                $("#fileForm").ajaxSubmit({
                    url: baseUrl + "/images",
                    success: function (data) {
                        if (data.status == 1) {
                            //图片上传成功
                            $("#coverUrl").attr("src",data.url);//把背景图片存入sessionStorage
                        }
                    }
                });
            }
        });

        //出行时间
        $("#detailForm input[name='goTime']").click(function () {
            WdatePicker({
                readOnly: true,
                 minDate: new Date()
            });
        });

        var url;
        if (companionId) {
            url = baseUrl + "/companions/" + companionId;
            $("input[name='_method']").val("put");
        }else {
            url = baseUrl + "/companions";
            $("input[name='_method']").val("post");
        }

        //设置为异步请求表单
        $("#detailForm").ajaxForm({
            beforeSubmit: function (arr) {
                //console.log(arr);arr是代表提交表单时带的参数以及对应的参数值
                //在提交表单之前添加额外的参数
                arr.push({name: "title", value: $("#title").val()});
                arr.push({name: "coverUrl", value: $("#coverUrl").attr('src')});
                arr.push({name: "companionContent.content", value: Edr.getContent()});
                arr.push({name: "author.id", value: user.id});
                // arr.push({name:"state",value:state});
            },
            url:url,
            success: function (data) {
                //保存成功回调函数
                if (data.success) {
                    $(document).dialog({
                        autoClose: 1000,
                        content: '发布成功'
                    });
                    setTimeout(function () {
                        window.location.href = "/mine/myCompanion.html";//我的结伴页面
                    }, 1600);
                }
            }
        });

        //发布
        $("#release").click(function () {
            $("#detailForm").submit();
        });
    });

</script>
<body style="background: url('/upload/2.bmp')">

<form action="/images" method="post" id="fileForm">
    <input type="file" name="file" style="display: none" id="fileBtn">
</form>

<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:window.history.go(-1)">
                <span><i class="fa fa-times"></i></span>
            </a>
        </div>
        <div class="col-5"></div>
        <div class="col" style="text-align: right;">
            <i class="fa fa-check" id="release"><span>发布</span></i>
        </div>
    </div>
</div>

<div class="travels form-group">
    <div class="cover">
        <img id="coverUrl" width="100%" height="200px" render-src="coverUrl"/>

        <button type="button" class="btn" id="changeBtn">
            <i class="fa fa-pencil-square-o"></i>
            <small>更换封面</small>
        </button>

    </div>
    <div class="title">
        <input class="form-control" id="title" placeholder="请输入标题" render-value="title"/>
    </div>
    <h6 href="#">请输入具体要求</h6>
    <div id="contentEditor" render-html="companionContent.content">
    </div>
    <hr/>

    <div class="container">
        <form id="detailForm" method="post">
            <input type="hidden" name="_method">
            <label>出行时间:</label>
            <input type="text" name="goTime" class="form-control" render-value="goTime">
            <label>出行天数:</label>
            <input type="number" name="days" class="form-control" render-value="days">
            <label>地区:</label>
            <input type="text" name="place" class="form-control" render-value="place">

        </form>
    </div>
    <hr/>
</div>
</body>

</html>